package com.example.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.model.dtos.PageResult;
import com.example.model.dtos.R;
import com.example.model.pojo.Materials;
import com.example.model.pojo.Supplier;
import com.example.service.MaterialsService;
import com.example.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 基础信息控制类
 */
@RestController
@RequestMapping("basic/materials/")
public class MaterialsController {

    @Autowired(required = false)
    MaterialsService materialsService;

    /**
     * 添加供应商
     */
    @RequestMapping("addMaterials")
    public R<String> addMaterials(@RequestBody Map<String,String> map){
        String name = map.get("name");
        String model = map.get("model");
        String manufacturer = map.get("manufacturer");
        String price = map.get("price");


        //判断参数是否为空
        if (StringUtils.isBlank(name)){
            return R.error("添加失败");
        }
        Materials materials = new Materials(null, name, model, manufacturer,Double.valueOf(price));

        Boolean flag = materialsService.addMaterials(materials);
        if (!flag){
            return R.error("添加失败");
        }

        return R.success("添加成功");
    }

    /**
     * 删除供应商
     */
    @RequestMapping("deleteMaterials")
    public R<String> deleteMaterials(@RequestBody Map<String,String> map){
        String id = map.get("myId");
        //判断参数是否为空
        if (StringUtils.isBlank(id)){
            return R.error("参数错误");
        }

        Boolean flag = materialsService.deleteMaterials(id);
        if (!flag){
            return R.error("删除失败");
        }

        return R.success("删除成功");
    }

    /**
     * 修改供应商信息
     */
    @RequestMapping("updateMaterials")
    public R<String> updateMaterials(@RequestBody Map<String,String> map){
        String id = map.get("myId");
        //判断参数是否为空
        if (StringUtils.isBlank(id)){
            return R.error("参数错误");
        }
        String name = map.get("name");
        String model = map.get("model");
        String manufacturer = map.get("manufacturer");
        String price = map.get("price");
        Materials materials = new Materials(id, name, model, manufacturer,Double.valueOf(price));



        Boolean flag = materialsService.updateMaterials(materials);
        if (!flag){
            return R.error("更新成功");
        }

        return R.success("更新成功");
    }

    /**
     * 查看供应商列表
     */
    @RequestMapping("MaterialsList")
    public R<Object> materialsList(@RequestBody Map<String,String> map){
        String pageStr = map.get("current");
        String pageSizeStr = map.get("size");
//        若为空，则查询第一页，每页10条
        if (StringUtils.isBlank(pageStr)){
            pageStr="1";
        }
        if (StringUtils.isBlank(pageSizeStr)){
            pageSizeStr="10";
        }
        Integer page = Integer.parseInt(pageStr);
        Integer pageSize = Integer.parseInt(pageSizeStr);


        PageResult pageResult = materialsService.materialsList(page,pageSize);
        return R.success(pageResult);
    }
}
